#include "GameActions.h"


void FadeAction::update( float elapsed ) {
	ds::Shader& shader = gEngine->getResourceManager().getShader(m_FadeHandle);
	m_Timer += elapsed;
	if ( m_Timer > FADING_TTL ) {
		m_Timer = FADING_TTL;
	}
	shader.setFloat("timer",m_Timer);
}

// ---------------------------------------------------
//  GameOverMenuAction
// ---------------------------------------------------
GameOverMenuAction::GameOverMenuAction(const std::string& materialName,const std::string& layerName,GameCore* core) 
	: ds::BasicMenuAction("GameOverMenu",materialName,layerName) , m_Core(core) {
		setMouseOffset(-15,-30);
		createTextNode("NormalText","XScale");
		addSprite(ds::Vec2(64,180),ds::Rect(800,0,895,120));
		addText("NormalText",310,360,"Points:");
		addText("NormalText",310,390,"Highest combo:");
		addText("NormalText",310,420,"Pieces left:");
		addText("NormalText",310,510,"Highscore:");
		addText("NormalText",310,450,"Total time:");

		addText("NormalText",590,360,"012345",20);
		addText("NormalText",590,390,"20",21);
		addText("NormalText",590,420,"32",22);
		addText("NormalText",590,510,"12000",23);
		addText("NormalText",590,450,"02:30",24);	

		addLink(130,"NormalText",360,580,310,18,"Back to main menu",ds::Color(1.0f,0.5f,0.0f,1.0f),"MainMenu");
		stop();
}

void GameOverMenuAction::start() {
	Score& fs = m_Core->score();
	setText("NormalText",20,590,360,ds::string::to_string(fs.points));
	setText("NormalText",21,590,390,ds::string::to_string(fs.highestCombo));
	setText("NormalText",22,590,420,ds::string::to_string(fs.piecesLeft));
	setText("NormalText",23,590,510,ds::string::to_string(fs.highscore));
	char buffer[10];
	sprintf(buffer,"%02d:%02d",m_Core->hud().getMinutes(),m_Core->hud().getSeconds());
	setText("NormalText",24,590,450,buffer);	
	BasicMenuAction::start();
}


// ---------------------------------------------------
//  HighscoreMenuAction
// ---------------------------------------------------
HighscoreMenuAction::HighscoreMenuAction(const std::string& materialName,const std::string& layerName,GameCore* core) 
	: ds::BasicMenuAction("HighscoreMenu",materialName,layerName) , m_Core(core) {
		setMouseOffset(-15,-30);
		createTextNode("NormalText","XScale");
		createTextNode("BigText","BigXScale");
		addSprite(ds::Vec2(55,150),ds::Rect(500,0,914,215));

		addText("BigText",360,400,"Highscores");

		addText("NormalText",360,460,"Easy");
		addText("NormalText",520,460,"0",34);

		addText("NormalText",360,490,"Medium");
		addText("NormalText",520,490,"0",35);

		addText("NormalText",360,520,"Hard");
		addText("NormalText",520,520,"0",36);

		addLink(130,"NormalText",360,580,310,18,"Back to main menu",ds::Color(1.0f,0.5f,0.0f,1.0f),"MainMenu");
		stop();
}

void HighscoreMenuAction::start() {
	setHighscore(4);
	setHighscore(5);
	setHighscore(6);	
	BasicMenuAction::start();
}

void HighscoreMenuAction::setHighscore(int level) {
	Highscore hs;
	int id = level + 30;
	int yp = 460 + 30 * (level-4);
	if ( m_Core->highscores().get(level,hs) ) {
		setText("NormalText",id,520,yp,ds::string::to_string(hs.points));
	}
	else {
		setText("NormalText",id,520,yp,"not yet defined");
	}
}
